/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.example.resourceStealing2;

import java.util.List;

import org.nudt.jCloud.infrastructure.Cluster;
import org.nudt.jCloud.infrastructure.Slot;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.simulation.Framework;
import org.nudt.jCloud.simulation.ScaleData;

public class HeuristicStealingRM extends StealingRM{

	public HeuristicStealingRM(String name, Cluster cluster, int totalSlotNum) {
		super(name, cluster);
		setTotalSlotNum(totalSlotNum);
	}
			
	public void stealOneSlot(Framework stealingFrwk, Framework stolenFrwk){
		Slot slot = null;
		for(SlotType slotType : stolenFrwk.getFreeSlots().keySet()){
			slot = stolenFrwk.getFreeSlots().get(slotType).get(0);
			stolenFrwk.getFreeSlots().get(slotType).remove(slot);
			if(stolenFrwk.getFreeSlots().get(slotType).size()<=0){
				stolenFrwk.getFreeSlots().remove(slotType);
			}
			break;
		}
		stealingFrwk.leaseSlot(slot);
	}
	
	@Override
	public void scaleResource(List<ScaleData> scaleDataList) {
		List<Framework > requestFrwkList = requestFrwk(scaleDataList);		
		if(isInitialized() == false){
			initSlotRatio();
		}
		lease(scaleDataList);		
		
		stealFromNoReq(requestFrwkList, scaleDataList);
		stealFromReqSatisfied(scaleDataList);
		stealFromReqOverThresh(scaleDataList);			
	}
	
}
